package com.code.controller;
import com.alibaba.fastjson.JSONObject;
import com.code.domain.Address;
import com.code.domain.Dish;
import com.code.domain.ListData;
import com.code.domain.Type;
import com.code.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
public class DishController {
    @Autowired
    DishService dishService;
    @RequestMapping(value = "dish/add")
    public Object addDish (String dishName, double price , Type dishType,@RequestParam("image") MultipartFile file){
        JSONObject jsonObject=new JSONObject();
        //处理图片文件转成url
        if (file.isEmpty()) {
            jsonObject.put("code" ,"未上传图片");
            return jsonObject;
        }
        String fileName = System.currentTimeMillis()+file.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "dishPic";
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }
        File destFile=new File(filePath+System.getProperty("file.separator")+fileName);
        try {
            file.transferTo(destFile);
        }catch(IOException e) {
            e.printStackTrace();
        }
        String imageUrl="http://localhost:8080/dishPic/"+fileName;
        /////
        if ( dishService.addDish(dishName, price, dishType,imageUrl) >0 ){
            jsonObject.put("code" ,"添加成功");
        }else {
            jsonObject.put("code", "添加失败");
        }
        return jsonObject;
    }
    @RequestMapping("dish/delete")
    public Object deleteDish (Integer dishId){
        JSONObject jsonObject=new JSONObject();
        if ( dishService.deleteDish(dishId) >0 ){
            jsonObject.put("code" ,"删除成功");
        }else {
            jsonObject.put("code", "删除失败");
        }
        return jsonObject;
    }
    @RequestMapping("dish/update")
    public Object updateDish (String dishType,Dish dish,@RequestParam(value = "img" ,required = false)MultipartFile file){
        JSONObject jsonObject=new JSONObject();
        Optional<Integer> integerOptional=Optional.ofNullable(dish.getDishId());
        Optional<String> stringOptional=Optional.ofNullable(dishType);
        if (stringOptional.isPresent()){
            switch (dishType){
                case "STAPLE":
                    dish.setDishType(Type.STAPLE);
                    break;
                case "VEGETABLE":
                    dish.setDishType(Type.VEGETABLE);
                    break;
                case "DRINKS":
                    dish.setDishType(Type.DRINKS);
                    break;
                case "MEAT":
                    dish.setDishType(Type.MEAT);
                    break;
                default:
                    dishType=null;
            }
        }
        // 处理图片更新
        if (file==null) {
            dishService.updateDish(dish);

        }else{
            String fileName = System.currentTimeMillis()+file.getOriginalFilename();
            String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "dishPic";
            File file1 = new File(filePath);
            if (!file1.exists()){
                file1.mkdir();
            }
            File destFile=new File(filePath+System.getProperty("file.separator")+fileName);
            try {
                file.transferTo(destFile);
            }catch(IOException e) {
                e.printStackTrace();
            }
            String imageUrl="http://localhost:8080/dishPic/"+fileName;
            dish.setImage(imageUrl);

            ////

            if (integerOptional.isPresent() && dishService.updateDish(dish) >0 ){
                jsonObject.put("code","更新成功");
            }else {
                jsonObject.put("code", "更新失败");
            }
        }

        return jsonObject;
    }
    @RequestMapping("dish/search")
    public Object searchDish (Integer dishId){
        JSONObject jsonObject=new JSONObject();
        Dish dish=dishService.search(dishId);
        if (  dish!=null ){
            jsonObject.put("dish" ,dish);
        }else {
            jsonObject.put("dish", "无结果,请检查id");
        }
        return jsonObject;
    }
    @RequestMapping("dish/all")
    public Object allDish (){
        JSONObject jsonObject=new JSONObject();
        List<Dish> list=dishService.searchAll();
        if (list.isEmpty()){
            jsonObject=null;
        }else {
            jsonObject.put("list",list);
        }
        return jsonObject;
    }
    @RequestMapping("dish/allType")
    public Object allDishByType (){
        JSONObject jsonObject=new JSONObject();
        List<Dish> list1=dishService.searchByType("STAPLE");
        List<Dish> list2=dishService.searchByType("VEGETABLE");
        List<Dish> list3=dishService.searchByType("MEAT");
        List<Dish> list4=dishService.searchByType("DRINKS");
        ListData listData1=new ListData("主食",list1);
        ListData listData2=new ListData("素食",list2);
        ListData listData3=new ListData("荤食",list3);
        ListData listData4=new ListData("饮品",list4);
        List<ListData> allList=new ArrayList<>();
        allList.add(listData1);
        allList.add(listData2);
        allList.add(listData3);
        allList.add(listData4);
        if (allList.isEmpty()){
            jsonObject.put("list","list为空");
        }else {
            jsonObject.put("list",allList);
        }
        return jsonObject;
    }
}
